package com.dmgis.hndz.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dmgis.hndz.pojo.Ybab02a;
import com.dmgis.hndz.pojo.Ybab02aDto;
import com.dmgis.hndz.service.Ybab02aService;
import com.dmgis.hndz.mapper.Ybab02aMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author PC
* @description 针对表【ybab02a(模型配置表)】的数据库操作Service实现
* @createDate 2024-02-06 09:58:05
*/
@Service
@Transactional
public class Ybab02aServiceImpl extends ServiceImpl<Ybab02aMapper, Ybab02a>
    implements Ybab02aService{

    /**
     * 批量修改因子权重配置
     * @param ybab02as 需要修改的因子权重配置信息数组
     * @return 返回结果
     */
    @Override
    public boolean batchUpdate(Ybab02a[] ybab02as) {
        UpdateWrapper<Ybab02a> updateWrapper;
        for (Ybab02a ybab02a : ybab02as) {
            updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("ybab02a010", ybab02a.getYbab02a010()); // Ybab01a的主键字段
            // 将传入的对象所有非空属性都更新到数据库中
            boolean result = this.update(ybab02a, updateWrapper);
            if (! result) {
                return false;
            }
        }
        return true;
    }

    /**
     * 因子权重配置--地质分区降雨模型00001--新增/删除/修改
     * @param ybab02aDtos 地质分区降雨模型00001模型配置信息
     */
    @Override
    public boolean upRainfallModelFactorConfiguration(List<Ybab02aDto> ybab02aDtos) {
        List<String> gradeName = new ArrayList<>();
        List<String> gradeStart = new ArrayList<>();
        List<String> gradeEnd = new ArrayList<>();
        List<String> y1_1 = new ArrayList<>();
        List<String> y1_2 = new ArrayList<>();
        List<String> y2_1 = new ArrayList<>();
        List<String> y2_2 = new ArrayList<>();
        List<String> y3_1 = new ArrayList<>();
        List<String> y3_2 = new ArrayList<>();
        List<String> y4_1 = new ArrayList<>();
        List<String> y4_2 = new ArrayList<>();
        List<String> y5_1 = new ArrayList<>();
        List<String> y5_2 = new ArrayList<>();
        for (Ybab02aDto ybab02aDto : ybab02aDtos) {
            gradeName.add(ybab02aDto.getGradeName());
            gradeStart.add(ybab02aDto.getGradeStart());
            gradeEnd.add(ybab02aDto.getGradeEnd());
            y1_1.add(ybab02aDto.getY1_1());
            y1_2.add(ybab02aDto.getY1_2());
            y2_1.add(ybab02aDto.getY2_1());
            y2_2.add(ybab02aDto.getY2_2());
            y3_1.add(ybab02aDto.getY3_1());
            y3_2.add(ybab02aDto.getY3_2());
            y4_1.add(ybab02aDto.getY4_1());
            y4_2.add(ybab02aDto.getY4_2());
            y5_1.add(ybab02aDto.getY5_1());
            y5_2.add(ybab02aDto.getY5_2());
        }
        // 对gradeName进行排序处理
        class Grade {
            int index;
            String gradeStart;
            Grade (int index, String gradeStart) {
                this.index = index;
                this.gradeStart = gradeStart;
            }
            String getGradeStart() {
                return gradeStart;
            }
            int getIndex() {
                return index;
            }
        }
        // 创建临时数组存储索引和数字
        List<Grade> temp = new ArrayList<>();
        for (int i = 0; i < gradeStart.size(); i++) {
            temp.add(new Grade(i, gradeStart.get(i)));
        }
        // 对临时数组按照数字进行排序
        Collections.sort(temp, Comparator.comparing(Grade::getGradeStart));
        // 创建新的列表存储排序后的结果
        List<String> sortedGradeName = new ArrayList<>();
        for (Grade grade : temp) {
            sortedGradeName.add(gradeName.get(grade.getIndex()));
        }

        boolean gradeNameResult = updateYbab02a050ByYbab02a040("gradeName", sortedGradeName.stream().collect(Collectors.joining(",")));
        boolean gradeStartResult = updateYbab02a050ByYbab02a040("gradeStart", gradeStart.stream().mapToInt(Integer::parseInt).sorted().mapToObj(String::valueOf).collect(Collectors.joining(",")));
        boolean gradeEndResult = updateYbab02a050ByYbab02a040("gradeEnd", gradeEnd.stream().mapToInt(Integer::parseInt).sorted().mapToObj(String::valueOf).collect(Collectors.joining(",")));
        boolean y1_1Result = updateYbab02a050ByYbab02a040("y1_1", y1_1.stream().mapToInt(Integer::parseInt).sorted().mapToObj(String::valueOf).collect(Collectors.joining(",")));
        boolean y1_2Result = updateYbab02a050ByYbab02a040("y1_2", y1_2.stream().mapToInt(Integer::parseInt).sorted().mapToObj(String::valueOf).collect(Collectors.joining(",")));
        boolean y2_1Result = updateYbab02a050ByYbab02a040("y2_1", y2_1.stream().mapToInt(Integer::parseInt).sorted().mapToObj(String::valueOf).collect(Collectors.joining(",")));
        boolean y2_2Result = updateYbab02a050ByYbab02a040("y2_2", y2_2.stream().mapToInt(Integer::parseInt).sorted().mapToObj(String::valueOf).collect(Collectors.joining(",")));
        boolean y3_1Result = updateYbab02a050ByYbab02a040("y3_1", y3_1.stream().mapToInt(Integer::parseInt).sorted().mapToObj(String::valueOf).collect(Collectors.joining(",")));
        boolean y3_2Result = updateYbab02a050ByYbab02a040("y3_2", y3_2.stream().mapToInt(Integer::parseInt).sorted().mapToObj(String::valueOf).collect(Collectors.joining(",")));
        boolean y4_1Result = updateYbab02a050ByYbab02a040("y4_1", y4_1.stream().mapToInt(Integer::parseInt).sorted().mapToObj(String::valueOf).collect(Collectors.joining(",")));
        boolean y4_2Result = updateYbab02a050ByYbab02a040("y4_2", y4_2.stream().mapToInt(Integer::parseInt).sorted().mapToObj(String::valueOf).collect(Collectors.joining(",")));
        boolean y5_1Result = updateYbab02a050ByYbab02a040("y5_1", y5_1.stream().mapToInt(Integer::parseInt).sorted().mapToObj(String::valueOf).collect(Collectors.joining(",")));
        boolean y5_2Result = updateYbab02a050ByYbab02a040("y5_2", y5_2.stream().mapToInt(Integer::parseInt).sorted().mapToObj(String::valueOf).collect(Collectors.joining(",")));
        return gradeNameResult && gradeStartResult && gradeEndResult && y1_1Result && y1_2Result && y2_1Result && y2_2Result
                && y3_1Result && y3_2Result && y4_1Result && y4_2Result && y5_1Result && y5_2Result;
    }

    /**
     * 通过ybab02a040来修改模型编号为00001的ybab02a050
     * @param ybab02a040
     * @param ybab02a050
     * @return 结果
     */
    private boolean updateYbab02a050ByYbab02a040(String ybab02a040, String ybab02a050) {
        UpdateWrapper<Ybab02a> updateWrapper = new UpdateWrapper<>();
        Ybab02a ybab02a = new Ybab02a();
        updateWrapper.eq("ybab02a020", "00001").eq("ybab02a040", ybab02a040);
        ybab02a.setYbab02a050(ybab02a050);
        return this.update(ybab02a, updateWrapper);
    }
}




